// pages/Invitation/Invitation.js
var total_micro_second = 1;

/* 毫秒级倒计时 */
function countdown(that) {
  // 渲染倒计时时钟
  that.setData({
    clock: dateformat(total_micro_second)
  });

  if (total_micro_second <= 0) {
    that.setData({
      clock: "已经截止"
    });
    // timeout则跳出递归
    return;
  }
  setTimeout(function() {
    // 放在最后--
    total_micro_second -= 500;
    countdown(that);
  }, 500)
}

// 时间格式化输出，如3:25:19 86。每500ms都会调用一次
function dateformat(micro_second) {
  // 秒数
  var second = Math.floor(micro_second / 1000);
  // 小时位
  var hr = Math.floor(second / 3600 % 24);
  // 分钟位
  var min = Math.floor((second - Math.floor(second / 3600) * 3600) / 60);
  // 秒位
  var sec = (second - Math.floor(second / 3600) * 3600 - min * 60); // equal to => var sec = second % 60;

  var date = Math.floor(second / 3600 / 24)
  var data = {
    date,
    hr,
    min,
    sec
  }
  return data;
}
Page({

  /**
   * 页面的初始数据
   */
  data: {
    clock: '',
    hideShare: true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var globalData = getApp().globalData;
    var custId = globalData.custID;
    var url = globalData.apiurl + '/mobile/api/TeamBuy/GetTeamBuyInfo';
    var that = this
    wx.request({
      url: url,
      data: {
        custId: custId,
        unionStoreID: options.unionStoreID,
        teamId: options.teamId
      },
      headers: {
        'Content-Type': 'application/json'
      },
      success: function(res) {
        var json = JSON.parse(res.data.content);
        total_micro_second = Date.parse(json.Team.EndTime) - Date.parse(json.Now);
        countdown(that);
        var kongqueData = [];
        for (var i = json.UserList.length + 2; i <= json.Team.TeamNumber; i++) {
          kongqueData.push(i);
        }
        that.setData({
          kongque: kongqueData
        });

        that.setData({
          TeamLeader: json.TeamLeader,
          Team: json.Team,
          UserList: json.UserList,
          Shop: json.Shop,
          Product: json.Product,
          DiscountedPrice: 0,
          options: options,
          nowPrice: 0
        })
        wx.showLoading({
          title: '加载中...',
          mask: true
        });
        wx.request({
          url: globalData.apiurl + '/mobile/api/Product/GetTeamProductDetail',
          data: {
            custId: custId,
            productId: json.Product.ProductID,
            userId: wx.getStorageSync('UserInfo').UserID,
            unionShopID: options.unionStoreID
          },
          method: 'GET',
          header: {
            'X_ManuName': globalData.manuName
          },
          success: function(res) {
            wx.hideLoading();
            if (res.data.code == 1200) {
              var result = res.data.content;
              var TeamBuyCurrentPrice = result.ProductInfo.TeamBuyCurrentPrice
              var DiscountedPrice = (result.ProductInfo.CurrentPrice - TeamBuyCurrentPrice).toFixed(2);
              that.setData({
                nowPrice: TeamBuyCurrentPrice,
                DiscountedPrice: DiscountedPrice
              })





            }
          }
        })

      }
    });



  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },
  showShare: function() {
    this.setData({
      hideShare: false

    })

  },
  showImg: function() {
    this.setData({
      hideShare: true

    })
    var that = this;
    wx.previewImage({
      urls: [that.data.Product],
      success: res => {},
      fail: res => {

      }
    });


  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function(res) {
    var globalData = getApp().globalData;
    var custId = globalData.custID;
    var that = this
    this.setData({
      hideShare: true

    })
    return {
      title: '一起来拼团吧：' + that.data.Product.ProductName,
      imageUrl: that.data.Product.ProductImg,
      path: '/pages/Invitation/Invitation?custId=' + custId + "&unionStoreID=" + that.options.unionStoreID + "&teamId=" + that.options.teamId
    }
  }
})